package com.semac.client.componentes.semactable;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;
import com.semac.shared.model.SemanaAcademica;

public class SemanacCellTable extends CellTable<SemanaAcademica> implements Handler {
	private static final ProvidesKey<SemanaAcademica> KEY_PROVIDER = new ProvidesKey<SemanaAcademica>() {		
		@Override
		public Object getKey(SemanaAcademica item) {
			return item.getCodSemana();
		}
	};
	

	private SingleSelectionModel<SemanaAcademica> listenerSelecaoUnica = new SingleSelectionModel<SemanaAcademica>();
	
	
	private TextColumn<SemanaAcademica> descricao = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			String valor = object.getDescricao();
			if (valor != null && valor.length() > 100)
				valor = valor.subSequence(0, 100) + "...";
			return valor;
		}
	};
	
	private TextColumn<SemanaAcademica> dataIni = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			DateTimeFormat formatter = DateTimeFormat.getFormat("dd/MM/yyyy");
			if (object.getDataIni() != null)
				return formatter.format(object.getDataIni());
			else
				return "";
		}
	};

	private TextColumn<SemanaAcademica> dataFim = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			DateTimeFormat formatter = DateTimeFormat.getFormat("dd/MM/yyyy");
			if (object.getDataFim() != null)
				return formatter.format(object.getDataFim());
			else
				return "";
		}
	};
	
	
	
	private TextColumn<SemanaAcademica> dataInicioInscricao = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			DateTimeFormat formatter = DateTimeFormat.getFormat("dd/MM/yyyy");
			if (object.getInicioInscricao() != null)
				return formatter.format(object.getInicioInscricao());
			else
				return "";
		}
	};
	
	
	
	private TextColumn<SemanaAcademica> dataFimInscricao = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			DateTimeFormat formatter = DateTimeFormat.getFormat("dd/MM/yyyy");
			if (object.getFimInscricao() != null)
				return formatter.format(object.getFimInscricao());
			else
				return "";
		}
	};
	
	
	
	private TextColumn<SemanaAcademica> ativa = new TextColumn<SemanaAcademica>() {			
		@Override
		public String getValue(SemanaAcademica object) {
			if (object.isAtiva())
				return "Ativa";
			else
				return "Inativa";
		}
	};


	private SemanaAcademica selecionada = null;
	

	public SemanacCellTable() {
		super(KEY_PROVIDER);
		
		//constroi a ui
		this.addColumn(descricao, "Descrição da semana");
		this.addColumn(dataIni, "Data de início da semana");
		this.addColumn(dataFim, "Data de término da semana");
		this.addColumn(dataInicioInscricao, "Início das inscrições dos alunos");
		this.addColumn(dataFimInscricao, "Fim das inscrições dos alunos");
		this.addColumn(ativa, "Ativa");
		
		this.setSelectionModel(listenerSelecaoUnica);		
		this.listenerSelecaoUnica.addSelectionChangeHandler(this);
	}
	
	
		

	@Override
	public void onSelectionChange(SelectionChangeEvent event) {
		this.selecionada = listenerSelecaoUnica.getSelectedObject();
		this.fireEvent(new SemanaAcademicaSelectionEvent(this.selecionada));
	}

	
	public void clearSelection(){
		this.listenerSelecaoUnica.setSelected(this.listenerSelecaoUnica.getSelectedObject(), false);
//		this.listenerSelecaoUnica.setSelected(this.selecionada, (this.selecionada != null));
		this.fireEvent(new SemanaAcademicaSelectionEvent(null));
	}
	
}
